package com.lxyk.controller.statistic;

import com.github.pagehelper.PageInfo;
import com.lxyk.domain.StuStatisticVO;
import com.lxyk.domain.StuStatisticVO2;
import com.lxyk.domain.Student;
import com.lxyk.domain.StudentVO;
import com.lxyk.service.statistic.StuTaskService;
import com.lxyk.service.student.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

//学生作业率
@Controller
@RequestMapping("/statistic")
public class StuTaskController {
    @Autowired
    private StuTaskService stuTaskService;
    @Autowired
    private StudentService studentService;
    //班级某一天所有学生作业率数据
    @RequestMapping("/classStdTaskDate")
    public String classStdTaskDate(StuStatisticVO condition, HttpServletRequest request){
        Date lastDate = stuTaskService.getLastDate(condition.getCid());
        condition.setRecordDate(lastDate);
        return findByConditionTask(condition,1,10,request);
    }

    //分页数据查询:班级某一天所有学生作业率数据
    @RequestMapping("/classDayTaskByPage")
    public String classDayTaskByPage(StuStatisticVO condition,Integer pageNum,Integer pageSize, HttpServletRequest request){
        pageNum = pageNum!=null?pageNum:1;
        pageSize = pageSize!=null?pageSize:10;
        PageInfo<StuStatisticVO> pageInfo = stuTaskService.findClassDayTask(condition.getCid(), condition.getRecordDate(), pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("cid",condition.getCid());
        request.setAttribute("condition",condition);
        return  "data-zyedit";
    }

    //条件查询
    @RequestMapping("/findByConditionTask")
    public String findByConditionTask(StuStatisticVO stuStatisticVO,Integer pageNum,Integer pageSize, HttpServletRequest request){
        PageInfo<StuStatisticVO> pageInfo = stuTaskService.findByCondition(stuStatisticVO, pageNum, pageSize);
        System.out.println("pageInfo=" + pageInfo);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("cid",stuStatisticVO.getCid());
        request.setAttribute("condition",stuStatisticVO);
        return  "data-zyedit";
    }

    //修改(批量)
    @RequestMapping("/saveTask")
    public String saveTask(StuStatisticVO2 stuStatisticVO2,StuStatisticVO condition, Integer pageNum, Integer pageSize,HttpServletRequest request){
        //获取所有要修改的数据
        List<StuStatisticVO> list = stuStatisticVO2.getList();
        //获取其中一个学生数据
        StuStatisticVO stuStatisticVO = list.get(0);
        //获取id
        Integer id = stuStatisticVO.getId();
        //获取班级id
        Integer cid = stuStatisticVO.getCid();
        //获取日期
        Date recordDate = stuStatisticVO.getRecordDate();
        stuTaskService.save(cid,recordDate,list);
        if(condition.getCid()==null){
            condition.setCid(cid);
        }
        request.setAttribute("condition",condition);
        String page = "";
        if(id!=null){
            //回到修改页
            page = findByConditionTask(condition,pageNum,pageSize,request);
//            page = classDayTaskByPage(cid, recordDate, pageNum, pageSize, request);
        }else{
            //回到添加页
            page = zyAddPage(condition,pageNum,pageSize,request);
        }request.setAttribute("success","OK");
        return page;
    }

    //跳转到添加页面
    @RequestMapping("/tozyAdd")
    public String tozyAdd(Integer cid,HttpServletRequest request){
        //跳转
        request.setAttribute("cid",cid);
        return "data-zyadd";
//        return zyAddPage(cid,1,100,request);
    }

    //添加页面,分页查询
    @RequestMapping("/zyAddPage")
    public String zyAddPage(StuStatisticVO condition,Integer pageNum,Integer pageSize,HttpServletRequest request){
        pageNum = pageNum!=null?pageNum:1;
        pageSize = pageSize!=null?pageSize:10;
        //根据班级id查询学生信息
        PageInfo<StuStatisticVO> pageInfo = stuTaskService.findStudentByCidAndDate(condition.getCid(),condition.getRecordDate(),pageNum,pageSize);
        System.out.println(pageInfo);
        //保存学生信息
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("cid",condition.getCid());
        request.setAttribute("condition",condition);
        return "data-zyadd";
    }

    //判断日期是否可用
    @RequestMapping("/isZyDateRight")
    @ResponseBody
    public String isDateRight(Integer cid,Date recordDate){
        List<StuStatisticVO> list = stuTaskService.findClassDayTask(cid, recordDate);
        if (list!=null && list.size()>0){
            //日期对应有数据,不可用
            return "false";
        }else{
            //日期对应没有数据,可用
            return "true";
        }
    }

}
